package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		selectContact(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		manager.getModel().removeContact(index).addContact(contact);
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		selectContact(index);
		submitContactDeletion();
		returnToHomePage();
		manager.getModel().removeContact(index);
		return this;
	}
	
	
	//------------------------------------------------------------------------------
	
  public SortedListOf<ContactData> getUiContacts() {
	SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
	manager.navigateTo().mainPage();
	List<WebElement> checkboxes = driver.findElements(By.xpath("(//tr[position() !=last()]/td[2])"));
	for (WebElement checkbox : checkboxes) {
		String title = checkbox.getText();;
		String firstname = title;
		contacts.add(new ContactData().withFirstname(firstname));
	}
	return contacts;	
  }
  
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {	
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("byear"), contact.getByear());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("home"), contact.getHome());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		if (formType == CREATION) {
			//selectByText(By.name("new_group"), "group 1");
		} else {
			if (driver.findElements(By.name("new_group")).size()!=0) {
				throw new Error("Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home"));
		return this;
	}

	public ContactHelper selectContact(int index) {
		click(By.xpath("(//tr[" + (index + 2) + "]/td[7]/a/img)"));		
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));	
		return this;
	}

	public ContactHelper submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
		return this;
	}
		
}
